package com.fh.modules.ems.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.common.util.ConvertUtils;
import com.fh.modules.ems.entity.EmsHomeBanner;
import com.fh.modules.ems.service.IEmsHomeBannerService;
import com.fhMall.request.QueryEmsHomeBannerRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Title: Controller
 * @Description: 首页轮播广告表
 * @author： code generator
 * @date： 2019-09-28
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("首页轮播广告表")
public class EmsHomeBannerController {
    @Autowired
    private IEmsHomeBannerService emsHomeBannerService;


    /**
     * 移动端查询首页banner广告
     *
     * @param queryEmsHomeBannerRequest
     * @return
     */
    @GetMapping(value = "/ems/emsHomeBanner/queryAll",consumes = "application/json")
    @ApiOperation("移动端查询首页banner广告")
    @AutoLog(value = "移动端查询首页banner广告", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryAll( @RequestBody QueryEmsHomeBannerRequest queryEmsHomeBannerRequest) {
        EmsHomeBanner emsHomeBanner = new EmsHomeBanner();
        BeanUtils.copyProperties(emsHomeBanner, queryEmsHomeBannerRequest);
        QueryWrapper<EmsHomeBanner> queryWrapper = new QueryWrapper<EmsHomeBanner>();
        emsHomeBanner.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, emsHomeBanner);
        queryWrapper.le("start_time",new Date());
        queryWrapper.ge("end_time",new Date());
        return Result.ok(emsHomeBannerService.list(queryWrapper));
    }

    /**
     * 分页列表查询
     *
     * @param emsHomeBanner
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/ems/emsHomeBanner/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value = "首页轮播广告表分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(EmsHomeBanner emsHomeBanner,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<EmsHomeBanner> queryWrapper = new QueryWrapper<EmsHomeBanner>();
        emsHomeBanner.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, emsHomeBanner);
        Page<EmsHomeBanner> page = new Page<EmsHomeBanner>(pageNo, pageSize);
        IPage<EmsHomeBanner> pageList = emsHomeBannerService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param emsHomeBanner
     * @return
     */
    @PostMapping(value = "/ems/emsHomeBanner/add")
    @ApiOperation("添加")
    @AutoLog("添加首页轮播广告表")
    public Result add(@RequestBody EmsHomeBanner emsHomeBanner) {
        emsHomeBanner.setDelFlag(CommonConstant.DEL_FLAG_0);
        emsHomeBanner.setAddressType(1);
        emsHomeBannerService.save(emsHomeBanner);
        return Result.ok("添加成功");
    }

    /**
     * 编辑
     *
     * @param emsHomeBanner
     * @return
     */
    @PutMapping(value = "/ems/emsHomeBanner/edit")
    @ApiOperation("修改")
    @AutoLog("修改首页轮播广告表")
    public Result edit(@RequestBody EmsHomeBanner emsHomeBanner) {
        boolean ok = emsHomeBannerService.updateById(emsHomeBanner);
        return ok ? Result.ok("修改成功") : Result.error("修改失败");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/ems/emsHomeBanner/deleteBatch")
    @ApiOperation("批量删除")
    @AutoLog("批量删除首页轮播广告表")
    public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        emsHomeBannerService.deleteBatchEntityByDelFlag(ids);
        return Result.ok("批量删除成功");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/ems/emsHomeBanner/queryById")
    @ApiOperation("通过id查询")
    @AutoLog(value = "首页轮播广告表通过id查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryById(@RequestParam(name = "id", required = true) String id) {
        EmsHomeBanner emsHomeBanner = emsHomeBannerService.getById(id);
        return Result.ok(emsHomeBanner);
    }
}
